import LGXCOM from '../common/common.js'
import LXCoord from '../common/struct/coord.js'

import { LGXEnums } from '../common/sysenum.js'


import globalConst from '../common/globalConst.js'
var global = globalConst.GlobalConst;

import utilfuncs from '../common/tools/utilfuncs.js';
var funcs = utilfuncs.UtilFuncs;

class AlignGraphSvc {
    constructor(datamanger) {
        this.dataManager = datamanger;
    }

    align(kind) {
        var nCount = this.dataManager.getSelectCount();
        if (nCount < 2) {
            return -1;
        }
        let rst = this.getValidGraphs();
        this.dataManager.m_pOperStepService.record(1, rst.allGraph);
        var ret = this.alignGraphs(rst.validGraphs, kind);
        this.dataManager.m_pOperStepService.record(0, rst.allGraph);
        return ret;
    }

    getValidGraphs() {
        let allGraph = this.dataManager.getSelGraphs();
        let validGraphs = [];
        allGraph.forEach(p => {
            if (p.m_graphType == LGXEnums.LGXGraphType.TextGraph_type) {
                let parent = p.getParentGraph();
                if (!parent) {
                    //不是独立文本的，也不能参与批量对齐
                    validGraphs.push(p);
                }
            }
            else if (this.chkValidAlignGraph(p.m_graphType)) {
                validGraphs.push(p);
            }
        });
        return { allGraph, validGraphs };
    }

    chkValidAlignGraph(graphType) {
        let flag = true;
        switch (graphType) {
            case LGXEnums.LGXGraphType.LineGraph_type:
            case LGXEnums.LGXGraphType.CommBusGraph_type:
            case LGXEnums.LGXGraphType.Combine_type:
            case LGXEnums.LGXGraphType.FastToolPanel_type:
            case LGXEnums.LGXGraphType.MultiSel_Rect_Type:
                flag = false;
                break;

            default:
                flag = true;
                break;
        }

        return flag;
    }

    alignGraphs(graphList, kind) {
        switch (kind) {
            case "left":
                this.algin_left(graphList);
                break;

            case "right":
                this.algin_right(graphList);
                break;

            case "bottom":
                this.algin_bottom(graphList);
                break;

            case "top":
                this.algin_top(graphList);
                break;

            case "horn"://水平中线对齐
                this.algin_horn(graphList);
                break;

            case "vertical"://垂直中线对齐
                this.algin_vertical(graphList);
                break;

            case "horn_even"://水平等间距
                this.align_horn_even(graphList);
                break;

            case "vertical_even"://垂直等间距
                this.align_vertical_even(graphList);
                break;

            case "samesize":
                this.align_samesize(graphList);
                break;
        }
    }

    algin_left(graphList) {
        let sortGraphList = graphList.sort(function (p1, p2) {
            if ((p1.getZone().left - p2.getZone().left) > 0) {
                return 1;
            }
            else {
                return -1;
            }
        }
        );

        let x = 0;
        let y = 0;
        let offsetPt = { x, y };
        var scaling = this.dataManager.getScaling();
        var mapscale = this.dataManager.getMapScale();
        let standardGraph = sortGraphList[0];
        let stpos = standardGraph.getZone().left;
        for (let i = 1; i < sortGraphList.length; i++) {
            let graph = sortGraphList[i];
            offsetPt.x = stpos - graph.getZone().left;
            this.dataManager.recordRepaintScope(graph);
            graph.translate(offsetPt, mapscale, false);
            this.dataManager.recordRepaintScope(graph);
        }
    }

    algin_right(graphList) {
        let sortGraphList = graphList.sort(function (p1, p2) {
            if ((p1.getZone().right - p2.getZone().right) > 0) {
                return -1;
            }
            else {
                return 1;
            }
        }
        );

        let x = 0;
        let y = 0;
        let offsetPt = { x, y };
        var scaling = this.dataManager.getScaling();
        var mapscale = this.dataManager.getMapScale();
        let standardGraph = sortGraphList[0];
        let stpos = standardGraph.getZone().right;
        for (let i = 1; i < sortGraphList.length; i++) {
            let graph = sortGraphList[i];
            offsetPt.x = stpos - graph.getZone().right;
            this.dataManager.recordRepaintScope(graph);
            graph.translate(offsetPt, mapscale, false);
            this.dataManager.recordRepaintScope(graph);
        }
    }

    algin_top(graphList) {
        let sortGraphList = graphList.sort(function (p1, p2) {
            if ((p1.getZone().top - p2.getZone().top) > 0) {
                return -1;
            }
            else {
                return 1;
            }
        }
        );

        let x = 0;
        let y = 0;
        let offsetPt = { x, y };
        var scaling = this.dataManager.getScaling();
        var mapscale = this.dataManager.getMapScale();
        let standardGraph = sortGraphList[0];
        let stpos = standardGraph.getZone().top;
        for (let i = 1; i < sortGraphList.length; i++) {
            let graph = sortGraphList[i];
            offsetPt.y = stpos - graph.getZone().top;
            this.dataManager.recordRepaintScope(graph);
            graph.translate(offsetPt, mapscale, false);
            this.dataManager.recordRepaintScope(graph);
        }
    }

    algin_bottom(graphList) {
        let sortGraphList = graphList.sort(function (p1, p2) {
            if ((p1.getZone().bottom - p2.getZone().bottom) > 0) {
                return 1;
            }
            else {
                return -1;
            }
        }
        );

        let x = 0;
        let y = 0;
        let offsetPt = { x, y };
        var scaling = this.dataManager.getScaling();
        var mapscale = this.dataManager.getMapScale();
        let standardGraph = sortGraphList[0];
        let stpos = standardGraph.getZone().bottom;
        for (let i = 1; i < sortGraphList.length; i++) {
            let graph = sortGraphList[i];
            offsetPt.y = stpos - graph.getZone().bottom;
            this.dataManager.recordRepaintScope(graph);
            graph.translate(offsetPt, mapscale, false);
            this.dataManager.recordRepaintScope(graph);
        }
    }

    //水平中线对齐
    algin_horn(graphList) {
        let sortGraphList = graphList.sort(function (p1, p2) {
            if ((p1.getZone().getCnt().y - p2.getZone().getCnt().y) > 0) {
                return 1;
            }
            else {
                return -1;
            }
        }
        );

        let x = 0;
        let y = 0;
        let offsetPt = { x, y };
        var scaling = this.dataManager.getScaling();
        var mapscale = this.dataManager.getMapScale();
        let standardGraph = sortGraphList[0];
        let stpos = standardGraph.getZone().getCnt().y;
        for (let i = 1; i < sortGraphList.length; i++) {
            let graph = sortGraphList[i];
            offsetPt.y = stpos - graph.getZone().getCnt().y;
            this.dataManager.recordRepaintScope(graph);
            graph.translate(offsetPt, mapscale, false);
            this.dataManager.recordRepaintScope(graph);
        }
    }


    //垂直中线对齐
    algin_vertical(graphList) {
        let sortGraphList = graphList.sort(function (p1, p2) {
            if ((p1.getZone().getCnt().x - p2.getZone().getCnt().x) > 0) {
                return 1;
            }
            else {
                return -1;
            }
        }
        );

        let x = 0;
        let y = 0;
        let offsetPt = { x, y };
        var scaling = this.dataManager.getScaling();
        var mapscale = this.dataManager.getMapScale();
        let standardGraph = sortGraphList[0];
        let stpos = standardGraph.getZone().getCnt().x;
        for (let i = 1; i < sortGraphList.length; i++) {
            let graph = sortGraphList[i];
            offsetPt.x = stpos - graph.getZone().getCnt().x;
            this.dataManager.recordRepaintScope(graph);
            graph.translate(offsetPt, mapscale, false);
            this.dataManager.recordRepaintScope(graph);
        }
    }

    //水平方向垂直中心线等间距
    align_horn_even(graphList) {
        let count = graphList.length;
        if (count < 3) {
            return;
        }
        let sortGraphList = graphList.sort(function (p1, p2) {
            if ((p1.getZone().getCnt().x - p2.getZone().getCnt().x) > 0) {
                return 1;
            }
            else {
                return -1;
            }
        }
        );

        let x = 0;
        let y = 0;
        let offsetPt = { x, y };
        var scaling = this.dataManager.getScaling();
        var mapscale = this.dataManager.getMapScale();
        let standardGraph1 = sortGraphList[0];
        let standardGraph2 = sortGraphList[count - 1];
        let offsetLen = (standardGraph2.getZone().getCnt().x - standardGraph1.getZone().getCnt().x) / (count - 1);
        let preGraph = standardGraph1;
        for (let i = 1; i < sortGraphList.length; i++) {
            let graph = sortGraphList[i];
            offsetPt.x = offsetLen - (graph.getZone().getCnt().x - preGraph.getZone().getCnt().x);
            this.dataManager.recordRepaintScope(graph);
            graph.translate(offsetPt, mapscale, false);
            this.dataManager.recordRepaintScope(graph);
            preGraph = graph;
        }
    }

    align_vertical_even(graphList) {
        let count = graphList.length;
        if (count < 3) {
            return;
        }
        let sortGraphList = graphList.sort(function (p1, p2) {
            if ((p1.getZone().getCnt().y - p2.getZone().getCnt().y) > 0) {
                return 1;
            }
            else {
                return -1;
            }
        }
        );

        let x = 0;
        let y = 0;
        let offsetPt = { x, y };
        var scaling = this.dataManager.getScaling();
        var mapscale = this.dataManager.getMapScale();
        let standardGraph1 = sortGraphList[0];
        let standardGraph2 = sortGraphList[count - 1];
        let offsetLen = (standardGraph2.getZone().getCnt().y - standardGraph1.getZone().getCnt().y) / (count - 1);
        let preGraph = standardGraph1;
        for (let i = 1; i < sortGraphList.length; i++) {
            let graph = sortGraphList[i];
            offsetPt.y = offsetLen - (graph.getZone().getCnt().y - preGraph.getZone().getCnt().y);
            this.dataManager.recordRepaintScope(graph);
            graph.translate(offsetPt, mapscale, false);
            this.dataManager.recordRepaintScope(graph);
            preGraph = graph;
        }
    }

    align_samesize(graphList) {
        var mapscale = this.dataManager.getMapScale();
        let standardGraph = graphList[0];
        let stwid = standardGraph.getZone().width();
        let sthgt = standardGraph.getZone().height();
        for (let i = 1; i < graphList.length; i++) {
            let graph = graphList[i];
            this.dataManager.recordRepaintScope(graph);
            this.scale2DstSize(graph, stwid, sthgt, mapscale);
            this.dataManager.recordRepaintScope(graph);
        }
    }

    scale2DstSize(graph, stwid, sthgt, mapscale) {
        switch (graph.m_graphType) {
            case LGXEnums.LGXGraphType.MoldGraph_type:
                this.scaleDev2DstSize(graph, stwid, sthgt, mapscale);
                break;

            case LGXEnums.LGXGraphType.RectGraph_type:
                this.scaleRect2DstSize(graph, stwid, sthgt, mapscale);
                break;

            case LGXEnums.LGXGraphType.CircleGraph_type:
                this.scaleCircle2DstSize(graph, stwid, sthgt, mapscale);
                break;
        }
    }

    scaleDev2DstSize(graph, stwid, sthgt, mapscale) {
        let symbol = graph.getSymbol();
        if (!symbol) {
            return;
        }
        let symRect = symbol.getSymScope();
        let scalex = stwid / symRect.width();
        let scaley = sthgt / symRect.height();
        graph.setScaleX(scalex);
        graph.setScaleY(scaley);
        graph.calScope(mapscale);
    }

    scaleRect2DstSize(graph, stwid, sthgt, mapscale) {
        let cntPt = graph.getZone().getCnt();
        var pos = new LXCoord.MultiPos();

        let p1 = new LGXCOM.LGXPoint();
        p1.x = cntPt.x - stwid * 0.5;
        p1.y = cntPt.y + sthgt * 0.5;
        pos.addPoint(p1);

        let p2 = new LGXCOM.LGXPoint();
        p2.x = cntPt.x + stwid * 0.5;
        p2.y = cntPt.y + sthgt * 0.5;
        pos.addPoint(p2);

        let p3 = new LGXCOM.LGXPoint();
        p3.x = cntPt.x + stwid * 0.5;
        p3.y = cntPt.y - sthgt * 0.5;
        pos.addPoint(p3);

        let p4 = new LGXCOM.LGXPoint();
        p4.x = cntPt.x - stwid * 0.5;
        p4.y = cntPt.y - sthgt * 0.5;
        pos.addPoint(p4);
        graph.setPos(pos);
        graph.calScope(mapscale);
    }

    scaleCircle2DstSize(graph, stwid, sthgt, mapscale) {
        let cntPt = graph.getZone().getCnt();
        var pos = new LXCoord.CirclePos();

        pos.x = cntPt.x;
        pos.y = cntPt.y;

        pos.radius = sthgt * 0.5;

        graph.setPos(pos);
        graph.calScope(mapscale);
    }

}

export default { AlignGraphSvc }